<%@ page language="java" pageEncoding="UTF-8"%>
<%@include file="../../common/import.jsp"%>
<html>
  <head>
    <title></title>
	 <link href="${ctx}/resources/js/ztree/zTreeStyle.css" rel="stylesheet" type="text/css" />
     <script type="text/javascript" src="${ctx}/resources/js/ztree/jquery.ztree.core-3.4.min.js"></script>
	 <script type="text/javascript" src="${ctx}/resources/js/ztree/jquery.ztree.excheck-3.4.min.js"></script>
	 <script type="text/javascript">
	       var setting = {
				check: {
					enable: true,
					chkboxType: {"Y":"ps", "N":"ps"}
				},
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					onClick: zTreeOnClick
				}
			};

			var zNodes =${moduleFunJson};
			
			function zTreeOnClick(event, treeId, treeNode) {
				var zTree = $.fn.zTree.getZTreeObj("moduleTree");
				zTree.checkNode(treeNode, !treeNode.checked, true, true);
				return false;
			}
			
			$(function(){
				$.fn.zTree.init($("#moduleTree"), setting, zNodes);
			});
			function onSubmit(){
				var zTree = $.fn.zTree.getZTreeObj("moduleTree");
				var nodes = zTree.getCheckedNodes(true);
				var array = new Array();
				var moduleIds = "";
				var funcIds = "";
				for(var i=0; i<nodes.length; i++){
					if(nodes[i].isParent){
						moduleIds += nodes[i].id+",";
					}else{
						funcIds += nodes[i].id+",";
					}
				}
				if(moduleIds.length>0){
					moduleIds = moduleIds.substring(0,moduleIds.length-1);
		    	}
				if(funcIds.length>0){
					funcIds = funcIds.substring(0,funcIds.length-1);
		    	}
				$.blockUI();
				$.ajax({
	    			  url: "${ctx}/sys/role/saveRolePrivileges.do",   
	    		      data: {roleId: '${roleId}', moduleIds: moduleIds, funcIds: funcIds}, 
	    		      type: "POST",
	    		      success: function(data) {
	    		    	  $.unblockUI();
	    		    	  if(data.success==true){
	    		        		showSuccessMsg();
	    		        		parent.closeWindow();
	    		        	}else{
	    		        		showFailAlert(data.msg)
	    		        	}
	    		      },
	    		      error: function(xhr) {
	    		    	  $.unblockUI();
	    		    	  showFailAlert2(xhr);
	    		      }
	    		  })
			}
	 </script>
  </head>
  <body>
     <div class="easyui-layout" region="center" fit="true">
	     <div region="center" border="false" >
	        <ul id="moduleTree" class="ztree"></ul>
	     </div>
	     <div region="south" border="false" style="text-align: right; padding: 5px 5px 5px 0;">
				<a class="easyui-linkbutton" iconcls="icon-ok" href="javascript:void(0)" onclick="onSubmit();">提交</a>
				<a class="easyui-linkbutton" iconcls="icon-cancel" href="javascript:void(0)" onclick="parent.closeWindow();">取消</a>
		 </div>
	 </div>
  </body>
</html>
